package com.geecity.flowable.flowabledemo.controller;

import com.geecity.flowable.flowabledemo.entity.R;
import com.geecity.flowable.flowabledemo.service.DispatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/deployment")
public class DeploymentController {

    @Autowired
    private DispatchService dispatchService;

    @GetMapping("/list")
    public R list(@RequestParam String cangKuUuid) {
        return dispatchService.deploymentMessageList(cangKuUuid);
    }

    @GetMapping(value = "/deploy", produces = "text/event-stream;charset=UTF-8")
    public void deploy(@RequestParam String cangKuUuid, 
                      @RequestParam String jarName, 
                      HttpServletResponse response) throws IOException {
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");
        response.setHeader("X-Accel-Buffering", "no");
        
        dispatchService.deployment(cangKuUuid, jarName, response);
    }

    @GetMapping("/stop")
    public R stop(@RequestParam String cangKuUuid, @RequestParam String jarName) {
        return dispatchService.stopService(cangKuUuid, jarName);
    }
} 